package com.adminex.controller;


import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.standard.expression.Each;

import java.io.File;
import java.io.IOException;

/*
文件上传测试
 */
@Log4j2
@Controller
public class FormTestController {
    @GetMapping("/form_layouts")
    public String form_layouts(){
        return "form/form_layouts";
    }
    @PostMapping("/upload")
    public String upload(String email,
                         String username,
                         MultipartFile headerImg,
                         MultipartFile[] photos) throws IOException {

        log.info("上传的信息：email={},username={},headerImg={},photos={}",
                email,username,headerImg.getSize(),photos.length);
        if (!headerImg.isEmpty()){
            //获取文件名
            String filename = headerImg.getOriginalFilename();
            //文件存储地址
            headerImg.transferTo(new File("D:\\"+filename));
        }
        if (photos.length>0){
            for (MultipartFile photo:photos) {
                if (!photo.isEmpty()){
                    String filename = photo.getOriginalFilename();
                    photo.transferTo(new File("D:\\"+filename));
                }
            }
        }
        return "index";
    }
    @RequestMapping("/form_advanced_components")
    public String form_advanced_components(){
        System.out.println("---------------------------------");
        return "form/form_advanced_components";
    }
    @RequestMapping("/form_wizard")
    public String form_wizard(){
        System.out.println("---------------------------------");
        return "form/form_wizard";
    }
    @RequestMapping("/form_validation")
    public String form_validation(){
        System.out.println("---------------------------------");
        return "form/form_validation";
    }
    @RequestMapping("/editors")
    public String editors(){
        System.out.println("---------------------------------");
        return "form/editors";
    }
    @RequestMapping("/inline_editors")
    public String inline_editors(){
        System.out.println("---------------------------------");
        return "form/inline_editors";
    }
    @RequestMapping("/pickers")
    public String pickers(){
        System.out.println("---------------------------------");
        return "form/pickers";
    }
    @RequestMapping("/dropzone")
    public String dropzone(){
        System.out.println("---------------------------------");
        return "form/dropzone";
    }
}
